package com.spark.lola.logistics.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.spark.lola.logistics.constant.LogisticsConstant;
import com.spark.lola.logistics.request.UpdateOrderLocationRequest;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.mybatis.entity.BaseModel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: Zhi-peng wang
 * @date: 2020/04/26 15:02
 * 货运单信息
 **/
@Getter
@Setter
@AllArgsConstructor
@ApiModel(value = "OrderInfo", description = "货运单信息")
@TableName("logistics_order_info")
public class OrderInfo extends BaseModel {

    @ApiModelProperty(name = "creatorId", value = "创建人id")
    private Long creatorId;

    @ApiModelProperty(name = "carId", value = "车辆id")
    private Long carId;

    @ApiModelProperty(name = "carNo", value = "车牌号")
    private String carNo;

    @ApiModelProperty(name = "semitrailerNo", value = "挂车牌号")
    private String semitrailerNo;

    @ApiModelProperty(name = "carPhone", value = "外调车联系方式")
    private String carPhone;

    @ApiModelProperty(name = "no", value = "订单号")
    private String no;

    @ApiModelProperty(name = "pageNo", value = "纸厂编号")
    private String pageNo;

    @ApiModelProperty(name = "lineType", value = "线路类型", notes = "0-直达车，1-回头车")
    private Integer lineType;

    @ApiModelProperty(name = "isEmpty", value = "是否空车", notes = "0-否，1-是")
    private Integer isEmpty;

    @ApiModelProperty(name = "originType", value = "来源类型", notes = "0-采购，1-其他")
    private Integer originType;

    @ApiModelProperty(name = "productIds", value = "商品id字符集")
    private String productIds;

    @ApiModelProperty(name = "locationInfoId", value = "纸厂地址id")
    private Long locationInfoId;

    @ApiModelProperty(name = "startAreaIds", value = "发货地省市区Ids")
    private String startAreaIds;

    @ApiModelProperty(name = "startArea", value = "发货地省市区")
    private String startArea;

    @ApiModelProperty(name = "startAddress", value = "发货地详细地址")
    private String startAddress;

    @ApiModelProperty(name = "endAreaIds", value = "目的地省市区Ids")
    private String endAreaIds;

    @ApiModelProperty(name = "endArea", value = "目的地省市区")
    private String endArea;

    @ApiModelProperty(name = "endAddress", value = "目的地详细地址")
    private String endAddress;

    @ApiModelProperty(name = "transportTotal", value = "运输总量（t）")
    private BigDecimal transportTotal;

    @ApiModelProperty(name = "ratedFreight", value = "额定运费")
    private BigDecimal ratedFreight;

    @ApiModelProperty(name = "price", value = "单价")
    private BigDecimal price;

    @ApiModelProperty(name = "actualAmount", value = "实付金额")
    private BigDecimal actualAmount;

    @ApiModelProperty(name = "expensesExists", value = "货运支出单是否存在", notes = "0-不存在，1-以存在")
    private Integer expensesExists;

    @ApiModelProperty(name = "isRejected", value = "驳回标志", notes = "0-正常，1-驳回")
    private Integer isRejected;

    @ApiModelProperty(name = "isArchive", value = "是否归档", notes = "0-否，1-是")
    private Integer isArchive;

    @ApiModelProperty(name = "status", value = "货运单状态", notes = "0-待发货，1-已发货，2-已收货，3，，4-已完成，-1-已作废")
    private Integer status;

    @ApiModelProperty(name = "remark", value = "备注")
    private String remark;

    @ApiModelProperty(name = "creator", value = "创建人")
    @TableField(fill = FieldFill.INSERT)
    private String creator;

    @ApiModelProperty(name = "updator", value = "修改人")
    @TableField(fill = FieldFill.UPDATE)
    private String updator;

    @ApiModelProperty(name = "createTime", value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @ApiModelProperty(name = "updateTime", value = "修改时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    @ApiModelProperty(name = "originId", value = "来源单据ID，预留字段")
    private Long originId;

    @ApiModelProperty(name = "orderProductList", value = "货运单商品信息集合")
    @TableField(exist = false)
    private List<OrderProduct> orderProductList = new ArrayList<>();

    @ApiModelProperty(name = "driverInfo", value = "驾驶员信息")
    @TableField(exist = false)
    private ShiroUser driverInfo;

    @ApiModelProperty(name = "carInfo", value = "车辆信息")
    @TableField(exist = false)
    private CarInfo carInfo;

    @ApiModelProperty(name = "locationInfo", value = "纸厂信息")
    @TableField(exist = false)
    private LocationInfo locationInfo;

    @ApiModelProperty(name = "orderProcessList", value = "货运单流程信息集合")
    @TableField(exist = false)
    private List<OrderProcess> orderProcessList = new ArrayList<>();

    @ApiModelProperty(name = "locationInfoList", value = "地址库信息集合")
    @TableField(exist = false)
    private List<LocationInfo> locationInfoList = new ArrayList<>();

    public OrderInfo() {
        // 数值初始化
        this.transportTotal = new BigDecimal(0);
        this.ratedFreight = new BigDecimal(0);
        this.actualAmount = new BigDecimal(0);
        // 状态初始化赋值
        this.lineType = LogisticsConstant.OrderLineType.TO_CAR;
        this.isEmpty = LogisticsConstant.OrderIsEmpty.NO;
        this.originType = LogisticsConstant.OrderOriginType.OTHER;
        this.expensesExists = LogisticsConstant.ExpensesExists.NO_EXISTS;
        this.isRejected = LogisticsConstant.IsRejected.NO;
        this.isArchive = LogisticsConstant.IsArchive.NO;
        this.status = LogisticsConstant.OrderStatus.WAIT_SHIPMENTS;
    }

    public OrderInfo(Long id, Integer status) {
        this.id = id;
        this.status = status;
    }

    public OrderInfo(Long id, Integer expensesExists, Integer version) {
        this.id = id;
        this.expensesExists = expensesExists;
        this.setVersion(version);
    }

    public OrderInfo(Integer isArchive, Integer version, Long id) {
        this.id = id;
        this.isArchive = isArchive;
        this.setVersion(version);
    }

    public OrderInfo(String productIds, BigDecimal transportTotal) {
        this.productIds = productIds;
        this.transportTotal = transportTotal;
    }

    public OrderInfo(UpdateOrderLocationRequest updateOrderLocationRequest) {
        this.id = updateOrderLocationRequest.getId();
        if (updateOrderLocationRequest.getUpdateType() == 0) {
            this.startAreaIds = updateOrderLocationRequest.getAreaIds();
            this.startArea = updateOrderLocationRequest.getArea();
            this.startAddress = updateOrderLocationRequest.getAddress();
        } else {
            this.endAreaIds = updateOrderLocationRequest.getAreaIds();
            this.endArea = updateOrderLocationRequest.getArea();
            this.endAddress = updateOrderLocationRequest.getAddress();
        }
    }

}
